/*
 author:叶林
 Data:2018-08-08
 Function:由于系统模块不多，业务简单，所以所有的接口都在该文件下
 */


function recursion(list, pid) {

    var rtn = [];
    for (i in list) {

        if (list[i].parentid === pid) {
            rtn.push(list[i]);
            list[i].children = recursion(list, list[i].catid);

        }
    }
    return rtn;
}
/**
 * @function 增加记录
 * @author qiwen
 * @date 2018-08-08
 * @param
 */
exports.add = (model,param, callback) => {
    model.create(param).then(record => {
        callback(null, record);
    }).catch(err => {
        console.error('Unable to connect to the database:', err);
        callback(err, null);
    });
};


/**
 * @function 删除记录
 * @author yelin
 * @date 2018-08-08
 * @param
 */
exports.del=(model,param, callback) => {
    model.destroy({where:param}).then(record => {
        callback(null, record);
    }).catch(err => {
        console.error('Unable to connect to the database:', err);
        callback(err, null);
    });
}

/**
 * @function 分页查询
 * @author Eric
 * @date 2018-05-28
 * @param
 */
exports.findByPage = (model,param, callback) => {
 /*   model.findAndCountAll(param).then(record => {
        callback(null, record);
    }).catch(err => {
        console.error('Unable to connect to the database:', err);
        callback(err, null);
    });
*/
    let obj={}
    model.findAndCountAll(param).then(record => {
        obj.return='0'
        obj.code='0'
        obj.message='查询成功！'
        obj.rowcount=record.count
        obj.data=record.rows
        callback(null, obj);
    }).catch(err => {
        obj.return='-1'
        obj.code='-1'
        obj.message='查询失败'
        obj.data=err
        console.error('Unable to connect to the database:', err);
        callback(obj, null);
    });
}

/**
 * @function 查询所有记录
 * @author Eric
 * @date 2018-05-28
 * @param
 */
exports.find = (model,param, callback) => {
    model.findAll(param).then(record => {
        callback(null, record);
    }).catch(err => {
        console.error('Unable to connect to the database:', err);
        callback(err, null);
    });
}
/**
 * @function 修改记录
 * @author yelin
 * @date 2017=8-08-08
 * @param
 */
exports.update = (model,updateparams,criteria, callback) => {
    gps_role.update(updateparams,criteria).then(record => {
        callback(null, record);
    }).catch(err => {
        console.error('Unable to connect to the database:', err);
        callback(err, null);
    });
}


/**
 * @function 获取栏目数据（下属，递归调用）
 * @author yelin
 * @date 2018
 * @param parentid
 */
exports.getcatlist = (model,parentid,param,  callback) => {
    let sequelize=DbInit.sequelize
    let obj={}
    let arr=[]

    model.findAll(param).then(record => {
        if(record.length>0)
        {
            for(i in record) {
                arr.push(record[i]);
            }

            let tree = recursion(arr, parentid);
            obj.return='0'
            obj.code='0'
            obj.message='获取栏目数据成功'
            obj.data=tree
            callback(null, obj)
        }
        else
        {
            obj.return='0'
            obj.code='0'
            obj.message='无效的参数'
            obj.data=[]
            console.trace('报错位置');
            callback(obj, null);
        }
    }).catch(err => {
        console.error('Unable to connect to the database:', err);
        callback(err, null);
    });

}
